/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ecommerce.util.ejb;

import br.com.ecommerce.entity.IEntity;
import br.com.ecommerce.persistence.CrudPersistence;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.ejb.EJB;

/**
 *
 * @author rpateixeira
 */
public abstract  class AbstractService <T extends IEntity> {
    @EJB
    protected CrudPersistence persistence;

    protected Class<T> getEntityType(){
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }
    public T insert(T t){
        return this.persistence.insert(t);
    }
    public void remove(T t){
         this.persistence.remove(t);
    }
    public T update(T t){
        return this.persistence.update(t);
    }
    public List<T> findAll(){
        return this.persistence.find("SELECT oEntidade FROM "+getEntityType().getSimpleName()+" oEntidade", null);
    }
    public T findById(Long id){
        return this.persistence.getPojo(getEntityType(), id);
    }
}
